package com.itheima.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.PageVo;
import com.itheima.admin.dto.PageDto;
import com.itheima.admin.dto.RoleUserDto;
import com.itheima.admin.dto.UserDto;
import com.itheima.admin.mapper.UserMapper;
import com.itheima.admin.mapper.UserRoleMapper;
import com.itheima.admin.pojo.User;
import com.itheima.admin.service.IUserService;
import com.itheima.admin.vo.UserPageVo;
import com.itheima.admin.vo.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;
    @Override
    public UserVo queryById(String id) {
        User user = userMapper.selectById(id);
        UserVo userVo = new UserVo();
        if(user!=null)BeanUtils.copyProperties(user,userVo);
        return userVo;
    }

    @Override
    public boolean deleteById(String id) {
        return userRoleMapper.deleteById(id);
    }

    @Override
    public PageVo<UserPageVo> queryByPage(PageDto pageDto) {
        PageHelper.startPage(pageDto.getCurrentPage(),pageDto.getPageSize());
        List<User> pages =  userMapper.selectByPage(pageDto.getStatus(),
                pageDto.getQureyString());
        PageInfo<User> page = new PageInfo<>(pages);

        List<UserPageVo> list = page.getList().stream().map(user -> {
            UserPageVo userPageVo = new UserPageVo();
            BeanUtils.copyProperties(user,userPageVo);
            userPageVo.setStatus(user.getState());
            return userPageVo;
        }).collect(Collectors.toList());

        return new PageVo<UserPageVo>(list,Long.valueOf(page.getTotal()).intValue());
    }

    @Override
    public boolean addUser(UserDto userDto) {
        String id = UUID.randomUUID().toString();
        return userRoleMapper.addUser(
                id,
                userDto.getEmail(),
                userDto.getPassword(),
                userDto.getUserName(),
                userDto.getDeptId(),
                userDto.getGender(),
                userDto.getState(),
                userDto.getBirthday(),
                userDto.getJoinDate(),
                userDto.getTelephone());
    }

    @Override
    public boolean editUser(UserDto userDto) {
        return userRoleMapper.editUser(
                userDto.getUserId(),
                userDto.getEmail(),
                userDto.getPassword(),
                userDto.getUserName(),
                userDto.getDeptId(),
                userDto.getGender(),
                userDto.getState(),
                userDto.getBirthday(),
                userDto.getJoinDate(),
                userDto.getTelephone());
    }

    @Override
    public boolean roleUser(RoleUserDto roleUserDto) {

        return userRoleMapper.roleUser(roleUserDto.getUserId(),roleUserDto.getRoleIds());
    }
}
